<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name']" placeholder="请输入合同名称" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-multi-select-tag
                v-decorator="['agreementType']"
                dictCode="agreement_type"
                placeholder="请选择合同类型" />
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="签署日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择签署日期" v-decorator="['signTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['money']" placeholder="请输入金额" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="付款方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['payType']" placeholder="请输入付款方式" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="已收金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['receiveMoney']" placeholder="请输入已收金额" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="未收金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['uncollectMoney']" placeholder="请输入未收金额" style="width: 100%" disabled/>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['statue']" :trigger-change="true" dictCode="agreement_statue" placeholder="请选择状态" />
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="广告费" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['advertMoney']" placeholder="请输入广告费" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="商务负责人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select allowClear v-decorator="['businessUserId']" placeholder="请选择商务负责人">
                <a-select-option v-for="v in userList" :key="v.id">
                  {{ v.realname }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="实施成本" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['implementCost']" placeholder="请输入实施成本" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="硬件成本" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['hardwareCost']" placeholder="请输入硬件成本" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="验收时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择验收时间" v-decorator="['acceptTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="到期时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择到期时间" v-decorator="['expireTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="付款记录" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="agreementPayRecordTable.loading"
          :columns="agreementPayRecordTable.columns"
          :dataSource="agreementPayRecordTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="支出记录" :key="refKeys[1]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[1]"
          :loading="agreementExpendRecordTable.loading"
          :columns="agreementExpendRecordTable.columns"
          :dataSource="agreementExpendRecordTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="续费记录" :key="refKeys[2]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[2]"
          :loading="agreementRenewRecordTable.loading"
          :columns="agreementRenewRecordTable.columns"
          :dataSource="agreementRenewRecordTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
    <a-row v-if="showFlowSubmitButton" style="text-align: center;width: 100%;margin-top: 16px;"><a-button @click="handleOk">提 交</a-button></a-row>
  </a-spin>
</template>

<script>

  import pick from 'lodash.pick'
  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import { getUserList } from '@/api/order'

  export default {
    name: 'AgreementForm',
    mixins: [JEditableTableMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['agreementPayRecord', 'agreementExpendRecord', 'agreementRenewRecord', ],
        tableKeys:['agreementPayRecord', 'agreementExpendRecord', 'agreementRenewRecord', ],
        activeKey: 'agreementPayRecord',
        // 付款记录
        agreementPayRecordTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '支付日期',
              key: 'payTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '支付金额',
              key: 'payMoney',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '支付状态',
              key: 'payRecordStatue',
              type: FormTypes.select,
              dictCode:"pay_record_statue",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        // 支出记录
        agreementExpendRecordTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '金额',
              key: 'money',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '状态',
              key: 'payRecordStatue',
              type: FormTypes.select,
              dictCode:"pay_record_statue",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            }
          ]
        },
        // 续费记录
        agreementRenewRecordTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '金额',
              key: 'money',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '续费时间(月)',
              key: 'time',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/agreement/agreement/add",
          edit: "/agreement/agreement/edit",
          queryById: "/agreement/agreement/queryById",
          agreementPayRecord: {
            list: '/agreement/agreement/queryAgreementPayRecordByMainId'
          },
          agreementExpendRecord: {
            list: '/agreement/agreement/queryAgreementExpendRecordByMainId'
          },
          agreementRenewRecord: {
            list: '/agreement/agreement/queryAgreementRenewRecordByMainId'
          },
        }
      }
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
      this.getUserList();
    },
    methods: {
      // 获取 处理人列表
      async getUserList() {
        const { code, message, result } = await getUserList({roleCode:"business"})
        if (code != 200) {
          return this.$message.error(message)
        }
        this.userList = result
      },
      addBefore(){
        this.form.resetFields()
        this.agreementPayRecordTable.dataSource=[]
        this.agreementExpendRecordTable.dataSource=[]
        this.agreementRenewRecordTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'name','signTime','money','payType','receiveMoney','uncollectMoney','statue','advertMoney','businessUserId','implementCost','hardwareCost','acceptTime','expireTime','agreementType')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.agreementPayRecord.list, params, this.agreementPayRecordTable)
          this.requestSubTableData(this.url.agreementExpendRecord.list, params, this.agreementExpendRecordTable)
          this.requestSubTableData(this.url.agreementRenewRecord.list, params, this.agreementRenewRecordTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          agreementPayRecordList: allValues.tablesValue[0].values,
          agreementExpendRecordList: allValues.tablesValue[1].values,
          agreementRenewRecordList: allValues.tablesValue[2].values,
        }
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          })
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','signTime','money','payType','receiveMoney','uncollectMoney','statue','advertMoney','businessUserId','implementCost','hardwareCost','acceptTime','expireTime'))
      },

    }
  }
</script>

<style scoped>
</style>